package com.control.qct.businesssteward.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.base.BaseActivity;
import com.control.qct.businesssteward.base.BaseBean;
import com.control.qct.businesssteward.httputils.HttpUtils;
import com.control.qct.businesssteward.utils.DecimalFormatUtils;
import com.control.qct.businesssteward.utils.DialogUtil;
import com.control.qct.businesssteward.view.CustomNumberPicker;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 整单优惠
 * Created by Administrator on 2017/12/1 0001.
 */

public class AllDiscountsActivity extends BaseActivity {

    @BindView(R.id.tv_receivable)
    TextView mTvReceivable;//应收金额
    @BindView(R.id.tv_discount)
    TextView mTvDiscount;//折扣力度
    @BindView(R.id.tv_after_discount_price)
    TextView mTvAfterDiscountPrice;//折后价
    @BindView(R.id.tv_discount_price)
    TextView mTvDiscountPrice;//优惠金额
    @BindView(R.id.tv_payment)
    TextView mTvPayment;//结算

    private View mSelectDiscountView;//优惠折扣布局

    private Dialog mSelectDiscountDialog;//优惠折扣选择框
    private int maxValue;
    private int minValue;

    private double receivablePrice;
    private double mAfterPrice;
    private Map<String, String> mParams;
    private Bundle mBundle;

    public AllDiscountsActivity() {
        super(R.layout.activity_all_discounts);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        receivablePrice = extras.getDouble("price");
        mAfterPrice = receivablePrice;
    }

    @Override
    protected void initView() {
        setLeftIcon(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTitle("整单优惠");
        mTvReceivable.setText("￥" + DecimalFormatUtils.DecimalFormatTwo(receivablePrice));
        mTvAfterDiscountPrice.setText("￥" + DecimalFormatUtils.DecimalFormatTwo(receivablePrice));

    }


    @OnClick({R.id.tv_discount, R.id.tv_payment})
    public void onClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_discount://优惠
                setSelectDiscountDialog();
                break;
            case R.id.tv_payment://结算
                orderPreferential();
                break;
        }
    }

    /**
     * 结算
     */
    private void orderPreferential() {
        mParams = new HashMap<>();
        mParams.put("orderAmount", mAfterPrice + "");
        mParams.put("orderDiscount", mTvDiscount.getText().toString());
        mNetProtocol.orderPreferential(this, mParams, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                mBundle = new Bundle();
                mBundle.putSerializable("data", baseBean);
                startActivityAndFinish(PaymentActivity.class, mBundle);
            }

            @Override
            public void onFail(BaseBean baseBean) {
                showShortToast(baseBean.getMessage());
            }
        });
    }

    /**
     * 选择折扣弹窗
     */
    private void setSelectDiscountDialog() {
        mSelectDiscountView = View.inflate(this, R.layout.dialog_select_discont, null);
        mSelectDiscountDialog = DialogUtil.getDialog(this, mSelectDiscountView);
        CustomNumberPicker np1 = mSelectDiscountDialog.findViewById(R.id.np1);
        CustomNumberPicker np2 = mSelectDiscountDialog.findViewById(R.id.np2);
        TextView tvSure = mSelectDiscountView.findViewById(R.id.tv_sure);
        TextView tvCancel = mSelectDiscountView.findViewById(R.id.tv_cancel);
        //设置不可编辑
        np1.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        np2.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        //设置最大最小值
        np1.setMinValue(0);
        np1.setMaxValue(9);
        np2.setMinValue(0);
        np2.setMaxValue(9);
        np1.setValue(maxValue);
        np2.setValue(minValue);
        np1.setNumberPickerDividerColor(np1);
        np2.setNumberPickerDividerColor(np2);
        np1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int oldValue, int newValue) {
                maxValue = newValue;
            }
        });
        np2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int oldValue, int newValue) {
                minValue = newValue;
            }
        });
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTvDiscount.setText(maxValue + "." + minValue);
                mAfterPrice = receivablePrice * Float.parseFloat(mTvDiscount.getText().toString()) / 10;
                mTvAfterDiscountPrice.setText("￥" + DecimalFormatUtils.DecimalFormatTwo(mAfterPrice));
                mTvDiscountPrice.setText("￥" + DecimalFormatUtils.DecimalFormatTwo(receivablePrice - mAfterPrice));
                mSelectDiscountDialog.dismiss();
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mSelectDiscountDialog.dismiss();
            }
        });
    }
}
